<?php
/**
 * FastApp.
 * 8/14/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp\Api\Service\Comm;

use App\Fastapp\Model\Currency;

class CurrencyService
{
    public function list(array $param = []): array
    {
        $query = Currency::query()
            ->select([
                'id', 'name', 'symbol', 'icon', 'parent_id',
                'scan_url', 'type', 'withdrawal_min', 'withdrawal_max',
                'withdrawal_charge', 'deposit_min', 'precision',
                'trade_precision', 'is_deposit', 'is_trade',
                'is_contract', 'is_futures'
            ]);
        $map = ['status' => 1];
        if (!empty($param['type'])) $map['type'] = $param['type'];
        if ($map) $query->where($map);
        $list = $query->orderByDesc('sort')->get();
        if (empty($param['is_array'])) return $list->mapWithKeys(function ($item) use ($param) {
            $item->name = fastapp_lang_text($item->name);
            return [$item->symbol => $item];
        })->toArray();
        return $list->map(function ($item) {
            $item->name = fastapp_lang_text($item->name);
            return $item;
        })->toArray();
    }
}